package org.admin.controller;

import core.Data;
import core.data.DataResult;
import org.admin.entity.dto.MenuDto;
import org.admin.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/menu")
public class MenuController {
    @Autowired
    private MenuService service;
    @PostMapping("/create")
    public DataResult create(@RequestBody Map<String, Object> post){
        return Data.create(post)
                .<MenuDto>addHandle("params", data -> {
                    new MenuDto().validate(new String[] {
                            "id", "action", "*name", "*appId", "*parentId", "icon", "*display", "detail", "groupId",
                    }).check(data);
                }).addHandle("create", service::create)
                .start()
                .getResult();
    }
    @PostMapping("/remove")
    public DataResult remove(@RequestBody Map<String, Object> post){
        return Data.create(post)
                .<MenuDto>addHandle("params", data -> {
                        new MenuDto().validate(new String[] {
                                "*ids"
                        }).check(data);
                }).addHandle("remove", service::remove)
                .start()
                .getResult();
    }
    @GetMapping("/query")
    public DataResult query(@RequestParam Map<String, Object> get){
        return Data.create(get)
                .<MenuDto>addHandle("params", data-> {
                    new MenuDto().validate(new String[] {
                            "action", "name", "appId", "display", "groupId", "current", "size"
                    }).check(data);
                }).addHandle("query", service::query)
                .start().getResult();

    }

    @GetMapping("/fetch")
    public DataResult fetch(@RequestParam Map<String, Object> get){
        return Data.create(get)
                .<MenuDto>addHandle("params", data -> {
                   new MenuDto().validate(new String[] {
                           "*id"
                   }).check(data);
                }).addHandle("fetch", service::fetch)
                .start().getResult();
    }
    /*


    public DataResult info(){

    }





     */
}
